local huisu = fk.CreateSkill{
  name = "xiaobai__huisu"
}

Fk:loadTranslationTable{
  ["xiaobai__huisu"] = "毁诉",
  [":xiaobai__huisu"] = "出牌阶段限一次，你可选择一名有牌的其他角色依次对其执行：议事，拼点，视为使用【决斗】，"..
    "然后若你<a href=':xiaobai__huisu-detail'>赢</a>的次数大于对方，你可以重铸至多三张牌。"..
    "每执行一项，若有赢的角色，其获得因上一项双方展示或失去过的手牌。",
  ["@@huisu"] = "毁诉",
  ["@$huisu"] = "毁诉",
  ["huisu__recast"] = "毁诉：你可以重铸至多三张牌",
  [":xiaobai__huisu-detail"] = "拼点牌大于对方，在【决斗】唯一对对方造成伤害的角色为赢的角色。",
}

local U = require "packages.utility.utility"

huisu:addEffect("active", {
  card_filter = Util.FalseFunc,
  target_num = 1, 
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    return to_select ~= player and (not selected or #selected == 0) and to_select:getHandcardNum() > 0
  end,
  can_use = function (self, player)
    return player:usedSkillTimes(huisu.name,Player.HistoryPhase) < 1
  end,
  on_use = function (self, room, effect)
    local target = effect.tos[1]
    local player = effect.from
    local end_id = room.logic:getCurrentEvent().id
    local win = 0
    local other_win = 0
    local to_get = {}
    --议事
    local discussion = nil
    if player:getHandcardNum() > 0 then
      discussion = U.Discussion(player, {player,target}, huisu.name)
      if discussion then
        table.insertTable(to_get, discussion.results[player].toCards)
        table.insertTable(to_get, discussion.results[target].toCards) 
      end
    end
    if player.dead then
      return
    end
    --拼点
    local pindian = nil
    if player:getHandcardNum() > 0 then
      local pindian = player:pindian({target}, huisu.name)
      if player.dead then
        return
      end
      if pindian.results[target].winner and #to_get > 0 then
        if pindian.results[target].winner == player then
          win = win+1
          to_get = table.filter(to_get,function (element, index, array)
            return table.indexOf(player:getCardIds("h"), element) == -1
          end)
          room:obtainCard(player, to_get, true)
          to_get = {}
        elseif pindian.results[target].winner == target then
          other_win = other_win + 1
          to_get = table.filter(to_get,function (element, index, array)
            return table.indexOf(target:getCardIds("h"), element) == -1
          end)
          room:obtainCard(target, to_get, true)
          to_get = {}
        else
          to_get = {}
        end
      end
      --记录拼点牌
      local card = pindian.fromCard
      if card then
        if room.logic:getEventsByRule(GameEvent.MoveCards,1,function (e)
          for _, move in ipairs(e.data) do
            if move.from == player and move.moveReason == 1 then
              for _, info in ipairs(move.moveInfo) do
                if info.cardId == card.id then
                  return true
                end
              end
            end
          end
        end,end_id) then
          table.insertIfNeed(to_get, card)
        end
      end
      card = pindian.results[target].toCard
      if card then
        if room.logic:getEventsByRule(GameEvent.MoveCards,1,function (e)
          for _, move in ipairs(e.data) do
            if move.from == player and move.moveReason == 1 then
              for _, info in ipairs(move.moveInfo) do
                if info.cardId == card.id then
                  return true
                end
              end
            end
          end
        end,end_id) then
          table.insertIfNeed(to_get, card)
        end
      end
    end
    --使用【决斗】
    local duel = Fk:cloneCard("duel") 
    duel.skillName = huisu.name
    if player:canUse(duel) then
      if player.dead then
        return
      end
      local use = {
        from = player,
        tos = {target},
        card = duel,
      }
      room:useCard(use)
      if use.damageDealt then
        if use.damageDealt[player] and not use.damageDealt[target] then
          other_win = other_win+1
          to_get = table.filter(to_get,function (element, index, array)
            return table.indexOf(target:getCardIds("h"),element) == -1
          end)
          room:obtainCard(target, to_get, true, fk.ReasonPrey, player, huisu.name)
        elseif not use.damageDealt[player] and use.damageDealt[target] then
          win = win+1
          to_get = table.filter(to_get,function (element, index, array)
            return table.indexOf(player:getCardIds("h"),element) == -1
          end)
          room:obtainCard(player, to_get, true, fk.ReasonPrey, player, huisu.name)
        end
      end
    end
    if player.dead then return end
    if win > other_win then
      local recast = room:askToCards(player, {
        min_num = 1,
        max_num = 3,
        skill_name = huisu.name,
        prompt = "huisu__recast"});
      if recast and #recast > 0 then 
        room:recastCard(recast, player, huisu.name)
      end
    end
  end
})

return huisu